package com.xszx.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xszx.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.xszx.bean.LoginUser;

@Controller
@CrossOrigin
public class LoginController {

    @Autowired
    LoginService LoginService;

    @RequestMapping("/login")
    @ResponseBody
    public int login(@RequestParam String account, @RequestParam String password, HttpServletResponse response, RedirectAttributes remodel, Model model, HttpSession session) {

        System.out.println(account);
        LoginUser loginUser = LoginService.selectUserByName(account);
        if (loginUser == null) {
            return -2;//用户不存在
        } else {
            //	response.addHeader("REDIRECT","REDIRECT");
            //	response.addHeader("CONTEXTPATH", "/superIndex");
            if (loginUser.getAccount().equals(account) && loginUser.getPassword().equals(password)) {

                //request.setAttribute("loginUser",loginUser);
                session.setAttribute("loginUser", loginUser);
                if (loginUser.getPermission() == 0) {
                    //return "redirect:/superIndex";
                    return 0;
                } else if (loginUser.getPermission() == 1) {
                    //return "redirect:/manaIndex";
                    return 1;
                } else {
                    //return "redirect:/userIndex";
                    return 2;
                }

            } else {
                remodel.addFlashAttribute("msg", "账号或密码不正确");
                //return "redirect:/reLogin";
                return -1;//账号密码不正确
            }
        }

    }

    //重新登录
    @RequestMapping("/reLogin")
    public String relogin() {
        return "login.html";
    }

    //进入决策者主界面
    @RequestMapping("/superIndex")
    public String superIndex() {
        return "superIndex.html";
    }

    //进入人事专员员主界面
    @RequestMapping("/manaIndex")
    public String manaIndex() {
        return "manaIndex.html";
    }

    //进入员工主界面
    @RequestMapping("/userIndex")
    public String userIndex() {
        return "userIndex.html";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login.html";
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session, HttpServletRequest request) {
        request.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:login.html";
    }

    @RequestMapping("/toUpdatePassword")
    public String toUpdatePassword() {
        return "updatePassword.html";
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
    public int updatePassword(@RequestBody String data, Model model, HttpSession session) {
        String[] array = data.split("&");
        String[] array1 = {"", "", ""};
        for (int i = 0; i < array.length; i++) {
            int index = array[i].indexOf("=");
            // System.out.print(index);
            array1[i] = array[i].substring(index + 1);

        }
        String oldPassword = array1[0];
        String newPassword = array1[1];
        String rePassword = array1[2];

        LoginUser user = (LoginUser) session.getAttribute("loginUser");
        System.out.print(user.getPassword());
        if (!user.getPassword().equals(oldPassword)) {
            return 0;
        } else {
            if (newPassword.equals(oldPassword)) {
                return 1;
            } else if (!newPassword.equals(rePassword)) {
                return 2;
            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", user.getId());
                map.put("password", newPassword);
                LoginService.updatePassword(map);
                return -1;
            }

        }

    }
}
